package org.jjche.core;

import org.apache.tomcat.util.http.LegacyCookieProcessor;
import org.jjche.core.alarm.dd.AlarmDingTalkService;
import org.jjche.core.exception.GlobalExceptionHandler;
import org.jjche.core.filter.RequestWrapperFilter;
import org.jjche.core.property.CoreProperties;
import org.jjche.core.url.AutoPrefixUrlMapping;
import org.jjche.core.util.SpringContextHolder;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.*;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;

/**
 * <p>
 * 入口
 * </p>
 *
 * @author miaoyj
 * @version 1.0.0-SNAPSHOT
 * @since 2020-07-09
 */
@Import({GlobalExceptionHandler.class,
        CoreProperties.class, cn.hutool.extra.spring.SpringUtil.class,
        RequestWrapperFilter.class, AlarmDingTalkService.class})
@Configuration
@ComponentScan(basePackages = {"cn.hutool.extra.spring"})
@EnableAsync(proxyTargetClass = true)
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
@EnableScheduling
public class CoreAutoConfiguration implements WebMvcRegistrations {

    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return new AutoPrefixUrlMapping();
    }

    /**
     * <p>
     * 忽略tomcat的cookie问题
     * A cookie header was received
     * </p>
     *
    * @return /
     */
    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
        return tomcatServletWebServerFactory -> tomcatServletWebServerFactory.addContextCustomizers(
                (TomcatContextCustomizer) context -> { context.setCookieProcessor(new LegacyCookieProcessor());});
    }

}
